﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RptList : SessionPageBase
{
    public string strRptID
    {
        get { return (string)(this.ViewState["strRptID"] ?? ""); }
        set { this.ViewState["strRptID"] = value; }
    }

    public string strRptName
    {
        get { return (string)(this.ViewState["strRptName"] ?? ""); }
        set { this.ViewState["strRptName"] = value; }
    }

    public string strProcedureName
    {
        get { return (string)(this.ViewState["strProcedureName"] ?? ""); }
        set { this.ViewState["strProcedureName"] = value; }
    }

    public string strDatabaseName
    {
        get { return (string)(this.ViewState["strDatabaseName"] ?? ""); }
        set { this.ViewState["strDatabaseName"] = value; }
    }

    public string strASCXName
    {
        get { return (string)(this.ViewState["strASCXName"] ?? ""); }
        set { this.ViewState["strASCXName"] = value; }
    }

    public string strDateStart
    {
        get { return (string)(this.ViewState["strDateStart"] ?? ""); }
        set { this.ViewState["strDateStart"] = value; }
    }

    public string strDateEnd
    {
        get { return (string)(this.ViewState["strDateEnd"] ?? ""); }
        set { this.ViewState["strDateEnd"] = value; }
    }

    public string strHeader
    {
        get { return (string)(this.ViewState["strHeader"] ?? ""); }
        set { this.ViewState["strHeader"] = value; }
    }

    public string strFooter
    {
        get { return (string)(this.ViewState["strFooter"] ?? ""); }
        set { this.ViewState["strFooter"] = value; }
    }

    public string strTitle
    {
        get { return (string)(this.ViewState["strTitle"] ?? ""); }
        set { this.ViewState["strTitle"] = value; }
    }

    public DataSet GetDataSet(string id)
    {
        try
        {
            DataSet ds_Reports = Hipocrate.DB.DBConnection.GetData("ReportsCustom_Select", new string[] { "UserID", "ID" }, new object[] { this.UserID, id });
            SetSession("ds_Reports", ds_Reports);
            return ds_Reports;
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
            return null;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Reports_Load();
        }
        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

    protected void Reports_Load()
    {
        try
        {
			if (!string.IsNullOrEmpty(Request.QueryString["id"]))
				this.strRptID = Request.QueryString["id"].ToString();
			else
			{
				ExportTools.CommonExport.ErrorOut(this.Context, "Nu s-a putut determina ce raport a fost selectat!");
				return;
			}

			if (!string.IsNullOrEmpty(Request.QueryString["startDate"]))
				this.strDateStart = Request.QueryString["startDate"].ToString();
			else
			{
				ExportTools.CommonExport.ErrorOut(this.Context, "Nu ati selectat data de inceput...");
				return;
			}

			if (!string.IsNullOrEmpty(Request.QueryString["endDate"]))
				this.strDateEnd = Request.QueryString["endDate"].ToString();
			else
			{
				ExportTools.CommonExport.ErrorOut(this.Context, "Nu ati selectat data de sfarsit...");
				return;
			}

            DataSet ds = GetDataSet(this.strRptID);
            DataView dv0 = new DataView(ds.Tables[0]);
            DataView dv1 = new DataView(ds.Tables[1]);

            if (dv0.Count > 0)
            {
                dv0.RowFilter = "ID=" + strRptID;
                dv1.RowFilter = "ID=" + strRptID;
                this.strRptName = dv0[0]["Name"].ToString();
                this.strProcedureName = dv0[0]["ProcedureName"].ToString();
                this.strDatabaseName = dv0[0]["DatabaseName"].ToString();
            }

            TableRow tblRow = new TableRow();
            TableCell tblCell = new TableCell();
            tblRow.Cells.Add(tblCell);
            tblReports.Rows.Add(tblRow);

            ASP.reports_controls_report_template_ascx nomenclator = (ASP.reports_controls_report_template_ascx)Page.LoadControl("~/Reports/Controls/Report_Template.ascx");
            nomenclator.ID = "nomenclator_" + this.strRptName;
            nomenclator.strReportID = this.strRptID;
            nomenclator.strDatabaseName = this.strDatabaseName;
            nomenclator.strProcedureName = this.strProcedureName;
            nomenclator.strDateStart = this.strDateStart;
            nomenclator.strDateEnd = this.strDateEnd;
            nomenclator.strHeader = UnPackDateInfo(this.strHeader);
			nomenclator.strFooter = UnPackDateInfo(this.strFooter);
			nomenclator.strTitle = UnPackDateInfo(this.strTitle);
			nomenclator.arrayParams = new System.Collections.Specialized.NameValueCollection(this.Request.QueryString);
			nomenclator.arrayParams.Remove("id");
            tblCell.Controls.Add(nomenclator);
        }

        catch (Exception ex)
        {
            this.SetMessage(ex.Message, true);
        }
    }

	private string UnPackDateInfo(string input)
	{
		string result = input;
		if (result.Contains("&month"))
			result = result.Replace("&month", "<i>" + extractMonth(this.strDateStart.Substring(3, 2)) + "</i>");
		if (result.Contains("&year"))
			result = result.Replace("&year", "<i>" + this.strDateStart.Substring(6, 4) + "</i>");
		if (result.Contains("&dateStart"))
			result = result.Replace("&dateStart", "<i>" + this.strDateStart + "</i>");
		if (result.Contains("&dateEnd"))
			result = result.Replace("&dateEnd", "<i>" + this.strDateEnd + "</i>");
		return result;
	}

    public string extractMonth(string month)
    {
		int monthIndex;
		if(!int.TryParse(month, out monthIndex))
			return "";
		if ((monthIndex < 1) || (monthIndex > 12))
			return "";
		return System.Globalization.CultureInfo.GetCultureInfo(Tools.GetLanguage(this.Request)).DateTimeFormat.MonthNames[monthIndex - 1];
    }
}

